//
//  CLNewPWViewController.m
//  CareLink
//
//  Created by w on 16/8/18.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLNewPWViewController.h"
#import "CLAEViewController.h"
#import "CLForgetPWViewController.h"
#import "SVProgressHUD.h"
#import "CLDrawViewController.h"
#import "CLHomeViewController.h"
@interface CLNewPWViewController ()
{
    //视图
    UIView *bgView;
    //密码文本
    UITextField *passward;
}
@end
/**
 *  找回密码2/2
 */
@implementation CLNewPWViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"";
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(clickaddBtn)];
    [addBtn setImage:[UIImage imageNamed:@"goback_back_orange_on"]];
    [addBtn setImageInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
    addBtn.tintColor = [UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8];
    [self.navigationItem setLeftBarButtonItem:addBtn];
    
    [self createTextFields];
}
//提示设置新的密码
-(void)createTextFields
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 75, self.view.frame.size.width-90, 30)];
    label.text = @"请设置新的密码";
    label.textColor = [UIColor grayColor];
    label.textAlignment=UITextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    CGRect frame = [UIScreen mainScreen].bounds;
    bgView = [[UIView alloc]initWithFrame:CGRectMake(10, 110, frame.size.width-20, 50)];
    bgView.layer.cornerRadius = 3.0;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    //密码内容
    passward = [self createTextFielfFrame:CGRectMake(100, 10, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"6-20位字母或数字"];
    passward.clearButtonMode = UITextFieldViewModeWhileEditing;
    passward.secureTextEntry = YES;
    
    //密码label
    UILabel *phonelabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 12, 50, 25)];
    phonelabel.text = @"密码";
    phonelabel.textColor = [UIColor blackColor];
    phonelabel.textAlignment = UITextAlignmentLeft;
    phonelabel.font = [UIFont systemFontOfSize:14];
    
    //UIImageView* userImageView1 = [self createImageViewFrame:CGRectMake(20, 10, 25, 25) imageName:@"注册-手机号" color:nil];
    UIImageView* pwdImageView2 = [self createImageViewFrame:CGRectMake(20, 60, 25, 25) imageName:@"mm_normal" color:nil];
    //登录完成按钮
    UIButton *landBtn = [self createButtonFrame:CGRectMake(10, bgView.frame.size.height+bgView.frame.origin.y+30,self.view.frame.size.width-20, 37) backImageName:nil title:@"完成" titleColor:[UIColor whiteColor]  font:[UIFont systemFontOfSize:17] target:self action:@selector(okClick)];
    landBtn.backgroundColor = [UIColor colorWithRed:8 / 255.0 green:65 / 255.0 blue:130 / 255.0 alpha:0.8];
    landBtn.layer.cornerRadius = 5.0f;
    
    //添加label
    [bgView addSubview:passward];
    [bgView addSubview:phonelabel];
    [self.view addSubview:landBtn];
}

-(void)okClick
{
    //    if([passward.text isEqualToString:@""])
    //    {
    //        return;
    //    }
    //    else if (passward.text.length <6)
    //    {
    //        return;
    //    }
    
//    __weak解决循环引用
    __weak CLNewPWViewController * weakSelf = self;
    
   [[NSUserDefaults standardUserDefaults] setObject:passward.text forKey:KpassWord];
    
        NSLog(@"%@",self->passward.text);
        if(self->passward.text.length == 0)
        {
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"  提示  " message:@" 亲!请输入新的密码 " preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action){
                
            }];
            [ac addAction:confirm];
            
        }else{
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"  提示  " message:@" 亲!已经修改成功 " preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action){
                
                //保存新的密码
                [[NSUserDefaults standardUserDefaults] setObject:passward.text forKey:KpassWord];
                //修改isload
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:KisLoad];
                //push主界面
                [self.navigationController pushViewController:[[CLHomeViewController alloc]init] animated:YES];
            }];
            [ac addAction:confirm];
            [self presentViewController:ac animated:true completion:^{
                
            }];
        }
    }
#pragma mark 后期添加的修改成功提示
//    [SVProgressHUD showSuccessWithStatus:@"修改成功"];
    //push主界面
//    [self.navigationController pushViewController:[[CLDrawViewController alloc]init] animated:YES];
//}

-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField = [[UITextField alloc]initWithFrame:frame];
    
    textField.font = font;
    
    textField.textColor = [UIColor grayColor];
    
    textField.borderStyle = UITextBorderStyleNone;
    
    textField.placeholder = placeholder;
    
    return textField;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image = [UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor = color;
    }
    
    return imageView;
}

-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}
//pop跳转
-(void)clickaddBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
